#include "cutscene_transition.h"

#include "graphics/graphics.h"
#include "graphics/graphics_util.h"

#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    

using namespace gameflow;
namespace {
/////////
// Fade
class CutsceneTransitionFade : public CutsceneTransition
{
public:
	CutsceneTransitionFade(bool reverse): CutsceneTransition(reverse) {};
	~CutsceneTransitionFade(){};
	virtual void	Render()
	{
		Graphics::Vertex2D*	VertA = Graphics::GetTempVert();
		int		Alpha;
		if (!m_Reverse)
		Alpha = int(m_Timer.GetProgress() * 255);
		else
		Alpha = int((1 - m_Timer.GetProgress()) * 255);
		UTIL_CLAMP(Alpha,0,255);

		COLOR	Col = ColorRGBA(0, 0, 0, Alpha);
		Graphics* pG = GetGraphics();
		GraphicsUtil::FillQuad(math::Vec2(0,0), math::Vec2(1,1), math::Vec2(0,0), math::Vec2(1,1), Col, VertA);
		pG->SetTexture(0, NULL);
		pG->DrawVertex2D(VertA, 6);
	}
};



} // anon namespace

////////////////////////////////////////
CutsceneTransition::CutsceneTransition(bool reverse)
{
	m_Reverse = reverse;
	m_Timer.SetLimit(0.4f); 
};
CutsceneTransition::~CutsceneTransition(){};
void	CutsceneTransition::Render(){};

bool	CutsceneTransition::IsReady()
{
	return m_Timer.IsReady();
};

void	CutsceneTransition::OnEnter()
{
};

void	CutsceneTransition::OnExit()
{
};

bool	CutsceneTransition::Tick(float dt)
{ 
	m_Timer.Tick(dt);

	if (IsReady()) OnExit();

	return IsReady();
};

std::string CutsceneTransition::TransitionTypeToName(CUTSCENE_TRANSITION t)
{
	switch (t)
	{
		case TRANSITION_FADEIN:			return "fadein";
		case TRANSITION_FADEOUT:		return "fadeout";
	}
	return "none";
};

CutsceneTransition::CUTSCENE_TRANSITION CutsceneTransition::TransitionNameToType(const std::string name)
{
	for (unsigned int i=0; i<TRANSITION_C; i++)
	{
		if (strcmpi(TransitionTypeToName((CUTSCENE_TRANSITION)i).c_str(), name.c_str())==0) return (CUTSCENE_TRANSITION)i;
	}
	return TRANSITION_NONE;
};

CutsceneTransition* CutsceneTransition::GetTransition(const std::string name)
{
	switch (TransitionNameToType(name))
	{
		case CutsceneTransition::TRANSITION_FADEIN:				return new CutsceneTransitionFade(true);
		case CutsceneTransition::TRANSITION_FADEOUT:			return new CutsceneTransitionFade(false);
	}
	return NULL;
};
